package com.dlwx.firefighting.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.activity.GoodsDetailsActivity;
import com.dlwx.firefighting.activity.ProductListActivity;
import com.dlwx.firefighting.adapter.GridClassifyAdapter;
import com.dlwx.firefighting.adapter.ProductAdapter;
import com.dlwx.firefighting.bean.AdBean;
import com.dlwx.firefighting.bean.BaseBean;
import com.dlwx.firefighting.bean.ClassifyBean;
import com.dlwx.firefighting.bean.GoodsBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.listener.RecyclerviewItemClickListener;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.views.GridSpacingItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/17.
 */

public class HomeClassifyFragment extends Fragment {
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    Unbinder unbinder;
    private ProductAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private CommonHeader header;
    private ImageView banner;
    private RecyclerView gridRecyclerView;
    private int tId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_classify, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        Bundle bundle=getArguments();
        tId=bundle.getInt("flag");
        getDatas();
        getSonlist();
        return view;
    }

    private void initView() {
        mAdapter = new ProductAdapter(getActivity());
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter.setDataList(new ArrayList<GoodsBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        //divider
        GridSpacingItemDecoration gridSpacingItemDecoration=new GridSpacingItemDecoration(2,20,true);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(gridSpacingItemDecoration);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(getActivity(), GoodsDetailsActivity.class);
                i.putExtra("id",mAdapter.getDataList().get(position).getProductId()+"");
                startActivity(i);
            }
        });
        addHeader();
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);
    }

    private void getDatas() {
        OkGo.<String>post(Constant.sonprolist)
                .tag(this)
                .params("id", tId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        List<GoodsBean> beans = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<BaseBean<GoodsBean,GoodsBean>>() {
                        }.getType());
                        mAdapter.addAll(beans);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
    private void getSonlist(){
        OkGo.<String>post(Constant.sonlist)
                .tag(this)
                .params("id", tId)
//                .params("num", 6)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        final List<ClassifyBean> beans = DealResult.getInstance().dealDatas(getActivity()
                                , response, new TypeToken<BaseBean<ClassifyBean,ClassifyBean>>(){}.getType());
                        GridClassifyAdapter gridClassifyAdapter=new GridClassifyAdapter(getActivity(), beans, new RecyclerviewItemClickListener() {
                            @Override
                            public void onItemClickListener(View v, int position) {
                                ClassifyBean bean = beans.get(position);
                               Intent i=new Intent(getActivity(), ProductListActivity.class);
                               i.putExtra("id",bean.getTid());
                               i.putExtra("name",bean.getName());
                               startActivity(i);
                            }
                        });
                        GridLayoutManager manager=new GridLayoutManager(getActivity(),2);
                        manager.setOrientation(GridLayoutManager.HORIZONTAL);
                        gridRecyclerView.setLayoutManager(manager);
                        gridRecyclerView.setAdapter(gridClassifyAdapter);
//                        gridRecyclerView.listener
//
                        AdBean bean = DealResult.getInstance().dealData(getActivity()
                                , response, new TypeToken<BaseBean<AdBean,AdBean>>(){}.getType());
                        if(bean!=null){
                            Glide.with(getActivity()).load(bean.getImg_url()).into(banner);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void addHeader() {
        header = new CommonHeader(getActivity(), R.layout.header_home_classify);
        banner = (ImageView) header.findViewById(R.id.banner);
        gridRecyclerView= (RecyclerView) header.findViewById(R.id.classify_recyclerView);
        lRecyclerViewAdapter.addHeaderView(header);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
